<?php

function mysend($socket, $msg) {
  if (substr($msg, -2) != "\r\n") {
    $msg .= "\r\n";    
  }
  $len = strlen($msg);
  $offset = 0;
  while ($offset < $len) {
    $sent = socket_write($socket, substr($msg, $offset), $len - $offset);
    if ($sent === false) {
      break;
    }
    $offset += $sent;
  }
  if ($offset < $len) {
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);
    echo "SENDING ERROR : $errormsg\n";
  } else {
  }
}

$login_message = array("VER 1 MSNP15 CVR0",
		       "CVR 2 0x0409 winnt 5.1 i386 MSNMSGR 8.5.1302 BC01 xudongsecret@hotmail.com",
		       "USR 3 SSO I xudongsecret@hotmail.com");


$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>0));
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>3, "usec"=>0));

if (!$connected = @socket_connect($socket, "messenger.hotmail.com", 1863)) {
  $error = socket_last_error();
  print_r($error);
 }


foreach ($login_message as $msg) {
  mysend($socket, $msg);
  $rect = socket_read($socket, '1024');
  if ($rect == '') {
    echo "empty\n";
    break;
  } else {
    echo $rect;
  }  
}

socket_close($socket);

?>
